<template>
	<view>
		<nav-bar v-if="module.navigationMessage" backState="2000" title="消息" :shadow="false">
			<!-- <view class="page_title" slot="left"></view> -->
			<!-- <view slot="right" class="search_box">传世草</view> -->
		</nav-bar>
		<nav-bar v-else title="消息" :shadow="false">
			<!-- <view class="page_title" slot="left"></view> -->
			<!-- <view slot="right" class="search_box">传世草</view> -->
		</nav-bar>
        <!-- 公共组件-每个页面必须引入 -->
        <public-module></public-module>
		<!-- <view class="unread_box">
			<text>1条未读消息</text>
			<text>清除未读</text>
		</view> -->
		<view class="message_box">
			<view class="message_select" @click="onPageJump('/pages/message/orderMessages')">
				<image src="../../static/icon/theme/ic_message_order.png" mode="aspectFill"></image>
				<view class="info_box">
					<text>订单通知</text>
					<text v-if="userInfo.orderMsgNum">{{userInfo.orderMsgNum}}</text>
				</view>
			</view>
			<view class="message_select" @click="onPageJump('/pages/message/systemMessages')">
				<image src="../../static/icon/ic_message_system.png" mode="aspectFill"></image>
				<view class="info_box">
					<text>系统通知</text>
					<text v-if="userInfo.sysMsgNum">{{userInfo.sysMsgNum}}</text>
				</view>
			</view>
		</view>
		<view class="session_msg_box">
			<conversation-item v-for="(item, index) of sessionList" :key="index" :item="item" :index="index" @delete="onDelete"></conversation-item>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import conversationItem from '@/components/module/conversation_item';
	export default {
		components: {
			conversationItem
		},
		data() {
			return {
				sessionList: [],
				pageNo: 1,
				pages: 1,
				module: {}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
			this.module = this.$base.module;
		},
		//页面显示
		onShow() {
			this.judgeLogin(() => {
				this.pageNo = 1;
				this.pages = 1;
				this.loadData();
			});
            uni.hideTabBarRedDot({
                index: 2
            });
		},
		//方法
		methods: {
			onPageJump(url) {
				this.judgeLogin(() => {
					uni.navigateTo({
						url: url
					});
				});
			},
            onDelete(index){
                this.sessionList.splice(index, 1);
            },
			loadData() {
				if (this.pageNo <= this.pages) {
					var httpData = {
						pageNo: this.pageNo,
						pageSize: 10
					};
					this.$http
						.post('api/im/session/v1/list', httpData, {
							load: false
						})
						.then(res => {
							uni.stopPullDownRefresh();
							this.pages = res.pages;
							res.data.map(item => {
								if (item.contentType == 'txt') {
									item.lastMessage = JSON.parse(item.lastMessage);
								}
								return item;
							});
							if (this.pageNo == 1) {
								this.sessionList = res.data;
							} else {
								this.sessionList = this.sessionList.concat(res.data);
							}
							if (res.data.length > 0) {
								this.pageNo = this.pageNo + 1;
							}
						});
				}
			}
		},

		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {
			this.judgeLogin(() => {
				this.pageNo = 1;
				this.pages = 1;
				this.loadData();
			});
		},
		//页面上拉触底
		onReachBottom() {
			this.loadData();
		},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import "@/style/mixin.scss";

	.message_box {
		background-color: #FFF;
		padding: 60rpx 0;
		display: flex;

		.message_select {
			display: flex;
			justify-content: center;
			align-items: center;
			flex: 1;

			image {
				width: 80rpx;
				height: 80rpx;
				flex-shrink: 0;
				margin-right: 30rpx;
			}

			.info_box {
				font-size: 28rpx;
				color: #333333;
				display: flex;
				align-items: center;
				text:nth-child(2) {
					height: 32rpx;
					line-height: 32rpx;
					min-width: 32rpx;
					text-align: center;
					padding: 0 6rpx;
					@include theme("gradient_bg");
					border-radius: 16rpx;
					font-size: 24rpx;
					color: #ffffff;
					margin-left: 15rpx;
				}
			}
		}
	}

	.page_title {
		font-size: 48rpx;
		color: #333333;
		font-weight: bold;
		margin-left: 30rpx;
	}

	.search_box {
		/* #ifdef MP */
		width: 400rpx;
		/* #endif */
		/* #ifndef MP */
		width: 548rpx;
		margin-right: 40rpx;
		/* #endif */
		height: 64rpx;
		background-color: #f7f7f7;
		border-radius: 32rpx;
		font-size: 28rpx;
		color: #cccccc;
		display: flex;
		align-items: center;
		padding: 0 30rpx;

		&:before {
			content: "";
			@include bis("../../static/icon/ic_search.png");
			width: 30rpx;
			height: 29rpx;
			margin-right: 20rpx;
		}
	}

	.unread_box {
		background-color: #ffffff;
		padding: 0 30rpx;

		text {
			font-size: 24rpx;
			color: #999999;
			margin-right: 40rpx;
		}
	}



	.session_msg_box {
		border-top: 2rpx solid #f5f5f5;
	}
</style>
